<script setup lang='ts'>
  import { onMounted, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { giftClassifyList, counterClassify } from '@/pinia/getter';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import { ElMessageBox } from 'element-plus';
  import Pagination from "@/components/Pagination/index.vue";
  import counterForm from './counterForm.vue';
  import lableList from './lableList.vue';
  const counterFormRef = ref(),
        lableListRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
    counterlabelClassify:[] as any[],
  })
  const listQuery = reactive({
    page: 1,
    limit: 10,
    counter_id: '',
    counter_cate_id: '',
  })
  const handleSerach = () => {
  listQuery.page = 1;
  getList();
};
const resetQuery = () => {
  Object.assign(listQuery, {
    page: 1,
    limit: 10,
    counter_id: '',
    counter_cate_id: '',
  });
};
  const handleForm = (row:any = {})=>{
    counterFormRef.value.openDialog({
      ...row,
      counterlabelClassify:action.counterlabelClassify
    })
  }
  const handleLableForm = ()=>{
    lableListRef.value.openDialog()
  }
  const handledel = (row:any={})=>{
    const { id='', name='' } = row
    if(!id) return ToastWarning('缺少货架id')
    ElMessageBox.confirm(`确定删除：${name}货架吗？`, '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      const loading = Toastloading()
      propApi.delCounter({id}).then(()=>{
        ToastSuccess('删除成功')
        getList()
        loading.close()
      }).catch(()=>{
        loading.close()
      })
    }).catch(() => { })
  }
  const counterCateSList = async(counter_id:number = 0)=>{
    listQuery.counter_cate_id = ''
    if(!counter_id) return action.counterlabelClassify = []
    const res = await propApi.getCounterCate({ counter_id })
    const { data } = res
    action.counterlabelClassify = data
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await propApi.getCounterList(listQuery)
    loading.close()
    const { data=[], total=0, } = res?.data || {}
    action.total = total
    action.list = data
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">货架类型：</div>
          <el-select v-model="listQuery.counter_id" @change="counterCateSList" placeholder="请选择货架类型">
            <el-option label="全部类型" :value="''" />
            <el-option
              v-for="item in counterClassify"
              :label="item.label"
              :value="item.type"
            />
          </el-select>
        </div>
        <div class="labelItem">
          <div class="label">货架标签：</div>
          <el-select v-model="listQuery.counter_cate_id" placeholder="请选择货架标签">
            <el-option
              v-for="item in action.counterlabelClassify"
              :key="item.counter_cate_id"
              :label="item.name"
              :value="item.counter_cate_id" />
          </el-select>
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="resetQuery">重置</el-button>
        </div>
      </div>
      <div class="labelBox">
        <div class="labelItem">
          <el-button type="primary" @click="handleForm()">新增货架商品</el-button>
          <el-button type="primary" @click="handleLableForm">新增货架标签</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="100" prop="id" label="货架ID" />
      <el-table-column width="100" sortable prop="order" label="排序" />
      <el-table-column width="120" prop="counter_name" label="货架类型" />
      <el-table-column width="100" prop="goods_id" label="礼物ID" />
      <el-table-column min-width="180" prop="gift_info.name" label="礼物名称" />
      <el-table-column width="120" prop="goods_type" label="礼物类型">
        <template #default="scoped">
          {{ giftClassifyList.find(res=> res.type == scoped.row.goods_type)?.label || '' }}
        </template>
      </el-table-column>
      <el-table-column width="180" show-overflow-tooltip prop="name" label="货架名称" />
      <el-table-column width="120" prop="counter_cate_name" label="货架标签" />
      <el-table-column min-width="280" show-overflow-tooltip prop="explain" label="货架说明" />
      <el-table-column fixed="right" width="180"  label="操作" >
        <template #default="scoped">
          <el-button type="primary" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" @click="handledel(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
      <counterForm ref="counterFormRef" @update-list="getList" />
      <lableList ref="lableListRef" />
  </div>
</template>